﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;

using Rmlife.Common;
using Rmlife.Data;
using Rmlife.Common.Generic;

namespace Rmlife.Control
{
    /// <summary>
    /// DataGrid 列表控件
    /// </summary>
    [DefaultProperty("Text"), ToolboxData("<{0}:DataGrid runat=server></{0}:DataGrid>")]
    public class DataGrid : System.Web.UI.WebControls.DataGrid, INamingContainer
    {
        private string ascImageUrl = "~/images/btn_up.gif";
        private string descImageUrl = "~/images/btn_down.gif";

        public string AscImageUrl
        {
            get
            {
                return this.ascImageUrl;
            }
            set
            {
                this.ascImageUrl = value;
            }
        }

        public string DescImageUrl
        {
            get
            {
                return this.descImageUrl;
            }
            set
            {
                this.descImageUrl = value;
            }
        }

        protected override void InitializeItem(DataGridItem item, DataGridColumn[] columns)
        {
            base.InitializeItem(item, columns);

            if ((this.AllowSorting) && (item.ItemType == ListItemType.Header))
            {
                for (int i = 0; i < item.Cells.Count; i++)
                {
                    if (columns[i].SortExpression != "")
                    {
                        item.Cells[i].Controls.Clear();
                        item.Cells[i].Text = "";

                        ImageButton button = new ImageButton
                        {
                            ImageUrl = this.AscImageUrl,
                            CommandArgument = columns[i].SortExpression,
                            CommandName = "ASC",
                            AlternateText = "按正序排序",
                            CausesValidation = false
                        };
                        button.Click += new ImageClickEventHandler(this.SortImgBtn_Click);

                        ImageButton button2 = new ImageButton
                        {
                            ImageUrl = this.DescImageUrl,
                            CommandArgument = columns[i].SortExpression,
                            CommandName = "DESC",
                            AlternateText = "按倒序排序",
                            CausesValidation = false
                        };
                        button2.Click += new ImageClickEventHandler(this.SortImgBtn_Click);


                        item.Cells[i].Controls.Add(button);
                        item.Cells[i].Controls.Add(new LiteralControl(columns[i].HeaderText));
                        item.Cells[i].Controls.Add(button2);
                    }
                }
            }
        }

        protected virtual void SortImgBtn_Click(object sender, ImageClickEventArgs e)
        {
            string[] sortFields = ((ImageButton)sender).CommandArgument.Split(new char[] { ',' });
            string sortName = ((ImageButton)sender).CommandArgument;
            string sortOrder = ((ImageButton)sender).CommandName;

            string sortExpression = this.GetSortExpression(sortFields, sortOrder);
            RecssDataGridSortCommandEventArgs args = new RecssDataGridSortCommandEventArgs(sender, sortExpression, sortOrder);
            this.OnSortCommand(args);
        }

        /// <summary>
        /// 跳转按钮
        /// </summary>
        public Rmlife.Control.Button GoToPagerButton = new Rmlife.Control.Button();

        /// <summary>
        /// 跳转文本框
        /// </summary>
        public System.Web.UI.HtmlControls.HtmlInputText GoToPagerInputText = new HtmlInputText();

        /// <summary>
        /// 构造函数
        /// </summary>
        public DataGrid()
            : base()
        {
            //this.GridLines=GridLines.Horizontal;
            //this.BorderWidth=0;
            //this.CellPadding=3;
            this.CssClass = "datalist";
            //this.ShowFooter=true;
            this.ShowHeader = true;
            this.AutoGenerateColumns = false;
            //this.PagerStyle.CssClass="datagridPager";
            //this.FooterStyle.CssClass="datagridFooter";
            this.SelectedItemStyle.CssClass = "datagridSelectedItem";
            this.ItemStyle.CssClass = "";// "datagridItem";
            this.HeaderStyle.CssClass = "category";
            this.PageSize = 25;
            this.PagerStyle.Mode = PagerMode.NumericPages;
            this.AllowCustomPaging = false;
            this.AllowPaging = true;
            this.AllowSorting = true;
            // this.DataKeyField = "ID";

            GoToPagerInputText.Attributes.Add("onfocus", "this.className='colorfocus';");
            GoToPagerInputText.Attributes.Add("onblur", "this.className='colorblur';");
            GoToPagerInputText.Attributes.Add("CssClass", "colorblur");

            this.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_ItemDataBound);
            this.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_ItemCreated);
            //this.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortGrid);
            GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sqlstring">查询字符串</param>
        public DataGrid(string sqlstring)
            : this()
        {
            BindData(sqlstring);
        }


        /// <summary>
        /// 加载默认列(编辑列和删除列)
        /// </summary>
        public void LoadDefaultColumn()
        {
            this.LoadEditColumn();
            this.LoadDeleteColumn();
        }


        /// <summary>
        /// 跳转按钮链接事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void GoToPagerButton_Click(object sender, System.EventArgs e)
        {
            if (this.GoToPagerInputText.Value != "")
            {

                if (!this.AllowCustomPaging)
                {


                    if (!Regex.IsMatch(this.GoToPagerInputText.Value, "^\\d+?\\d*$"))
                    {
                        LoadCurrentPageIndex(0);
                        return;
                    }
                    else
                    {
                        int gotoPager = Utils.StrToInt(this.GoToPagerInputText.Value.Trim(), 1);

                        if (gotoPager < 0)
                        {
                            LoadCurrentPageIndex(0);
                            return;
                        }

                        if (gotoPager > this.PageCount)
                        {
                            LoadCurrentPageIndex(this.PageCount - 1);
                        }
                        else
                        {
                            LoadCurrentPageIndex(gotoPager - 1);
                        }
                    }
                }
                else
                {
                    //设置当前DATAGRID的CurrentPageIndex属性
                    SetCurrentPageIndexByGoToPager();
                }
            }
        }

        /// <summary>
        /// 设置当前DATAGRID的CurrentPageIndex属性
        /// </summary>
        private void SetCurrentPageIndexByGoToPager()
        {
            if (this.GoToPagerInputText.Value != "")
            {

                if (!Regex.IsMatch(this.GoToPagerInputText.Value, "^\\d+?\\d*$"))
                {
                    this.CurrentPageIndex = 0;
                    return;
                }
                else
                {
                    int gotoPager = Utils.StrToInt(this.GoToPagerInputText.Value.Trim(), 1);

                    if (gotoPager < 0)
                    {
                        CurrentPageIndex = 0;
                        return;
                    }

                    if (gotoPager > PageCount)
                    {
                        CurrentPageIndex = this.PageCount - 1;
                    }
                    else
                    {
                        this.CurrentPageIndex = gotoPager - 1;
                    }
                }
            }
        }

        /// <summary>
        /// 加载编辑列
        /// </summary>
        public void LoadEditColumn()
        {
            EditCommandColumn ecc = new EditCommandColumn();//更新按钮列 
            ecc.SortExpression = "desc";
            ecc.ButtonType = ButtonColumnType.LinkButton;//链接按钮 
            ecc.EditText = "编辑";
            ecc.UpdateText = "更新";
            ecc.CancelText = "取消";
            ecc.ItemStyle.Width = 70;
            ecc.ItemStyle.BorderWidth = 1;
            ecc.ItemStyle.BorderStyle = BorderStyle.Solid;
            ecc.ItemStyle.BorderColor = System.Drawing.Color.FromArgb(234, 233, 225);
            this.Columns.AddAt(0, ecc);//增加按钮列 
        }

        /// <summary>
        /// 加载删除列
        /// </summary>
        public void LoadDeleteColumn()
        {
            ButtonColumn bc = new ButtonColumn();
            bc.SortExpression = "desc";
            bc.CommandName = "Delete";
            bc.Text = "删除";
            //bc.FooterText="添加";
            bc.ItemStyle.Width = 70;
            this.Columns.AddAt(1, bc);//增加按钮列 
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            if ((this.SourceDataTable != null))
            {
                BindData(this.SourceDataTable);
            }
            else
            {
                ;
            }
        }

        /// <summary>
        /// 绑定数据对象
        /// </summary>
        /// <param name="sqlstring">查询字符串</param>
        public void BindData(string sqlstring)
        {
            this.SqlText = sqlstring;
            DataTable dt = DbHelper.ExecuteDataset(CommandType.Text, sqlstring).Tables[0];
            BindData(dt);
        }

        public void BindData(DataTable dt)
        {
            this.SourceDataTable = dt;
            this.VirtualItemCount = dt.Rows.Count;

            /* DataView dv = dt.DefaultView;
             string sortExpression = (ViewState["SortExpression"] == null) ? String.Empty : ViewState["SortExpression"].ToString();
             string strOrder = (ViewState["SortOrder"] == null) ? String.Empty : ViewState["SortOrder"].ToString();
             if (!string.IsNullOrEmpty(sortExpression)) dv.Sort = sortExpression + " " + strOrder;
             */
            this.DataSource = this.SourceDataTable;
            this.DataBind();
        }

        public void BindData<T>(List<T> list)
        {
            this.VirtualItemCount = list.Count;
            this.DataSource = list;
            this.DataBind();
        }

        /// <summary>
        /// 排序列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SortGrid(Object sender, DataGridSortCommandEventArgs e)
        {

            SortTable(e.SortExpression, (DataTable)null);

            foreach (System.Web.UI.WebControls.DataGridColumn dc in this.Columns)
            {
                if (dc.SortExpression == e.SortExpression)
                {
                    if (dc.HeaderText.IndexOf("<img src=") >= 0)
                    {
                        if (this.DataGridSortType == "ASC")
                        {
                            dc.HeaderText = dc.HeaderText.Replace("<img src=" + this.ImagePath + "asc.gif height=13>", "<img src=" + this.ImagePath + "desc.gif height=13>");
                        }
                        else
                        {
                            dc.HeaderText = dc.HeaderText.Replace("<img src=" + this.ImagePath + "desc.gif height=13>", "<img src=" + this.ImagePath + "asc.gif height=13>");
                        }
                    }
                    else
                    {
                        if (this.DataGridSortType == "ASC")
                        {
                            dc.HeaderText = dc.HeaderText + "<img src=" + this.ImagePath + "desc.gif height=13>";
                        }
                        else
                        {
                            dc.HeaderText = dc.HeaderText + "<img src=" + this.ImagePath + "asc.gif height=13>";
                        }
                    }
                    //dc.. ..ItemStyle="sortColumn";
                    //dc.ItemStyle.BackColor = Color.AliceBlue;
                }
                else
                {
                    dc.HeaderText = dc.HeaderText.Replace("<img", "~").Split('~')[0];
                    //dc.ItemStyle.BackColor = Color.White;
                }
            }
        }

        /// <summary>
        /// 排序数据表
        /// </summary>
        /// <param name="SortExpression">排序表达式</param>
        /// <param name="dt">待排序的数据表</param>
        public void SortTable(string SortExpression, DataTable dt)
        {
            DataView dv = new DataView();
            if (dt != null && dt.Rows.Count > 0)
            {
                dv = new DataView(dt);
            }
            else
            {
                if (this.SourceDataTable != null)
                {
                    dv = new DataView(SourceDataTable);
                }
                else
                {
                    return;
                }
            }
            dv.Sort = SortExpression.Replace("<img", "~").Split('~')[0] + " " + this.DataGridSortType;
            this.DataSource = dv;
            this.DataBind();
        }

        /// <summary>
        /// 排序数据表
        /// </summary>
        /// <param name="SortExpression">排序表达式</param>
        /// <param name="sqlstring">查询字符串</param>
        public void SortTable(string SortExpression, string sqlstring)
        {
            DataView dv = new DataView();
            if (sqlstring != null && sqlstring != "")
            {
                dv = new DataView(DbHelper.ExecuteDataset(CommandType.Text, sqlstring).Tables[0]);
            }
            else
            {
                return;
            }

            dv.Sort = SortExpression.Replace("<img", "~").Split('~')[0] + " " + this.DataGridSortType;
            this.DataSource = dv;
            this.DataBind();
        }

        public void SortTable<T>(string sortExpression, List<T> list)
        {
            this.DataSource = list;
            this.Sort = sortExpression + " " + this.DataGridSortType;
            this.DataBind();
        }

        #region Property DataGridSortType

        /// <summary>
        /// 边框的宽度。
        /// </summary>
        [Description("表头的名称。"), Category("Appearance"), DefaultValue("ASC")]
        public string DataGridSortType
        {
            get
            {

                object obj = ViewState["DataGridSortType"];
                string ascordesc = obj == null ? "ASC" : (string)obj;
                if (ascordesc == "ASC")
                {
                    ViewState["DataGridSortType"] = "DESC";
                    return "DESC";
                }
                else
                {
                    ViewState["DataGridSortType"] = "ASC";
                    return "ASC";
                }

            }
            set
            {
                ViewState["DataGridSortType"] = value;
            }
        }
        /// <summary>
        /// 排序字段
        /// </summary>
        public string SortField
        {
            get
            {
                object obj = ViewState["SortField"];
                if (obj != null)
                    return obj.ToString();
                else
                    return string.Empty;
            }
            set
            {
                ViewState["SortField"] = value;
            }
        }

        #endregion


        private string sort;

        /// <summary>
        /// 排序字段
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public string Sort
        {
            get
            {
                /*
                object obj = ViewState["Sort"];
                if (obj != null)
                    return obj.ToString();
                else
                    return string.Empty;
              */
                return sort;
            }
            set
            {
                sort = value;
                SortTable(sort, (DataTable)null);
                //ViewState["Sort"] = value;
            }
        }

        /// <summary>
        /// 通过SQL字符串删除表记录并重新绑定
        /// </summary>
        /// <param name="sqlstring"></param>
        public void DeleteByString(string sqlstring)
        {
            DbHelper.ExecuteNonQuery(CommandType.Text, sqlstring);
            this.EditItemIndex = -1;
            BindData();
        }

        /// <summary>
        /// 编辑指定INDEX的记录
        /// </summary>
        /// <param name="itemindex"></param>
        public void EditByItemIndex(int itemindex)
        {
            this.EditItemIndex = itemindex;
            BindData();
        }

        /// <summary>
        /// 取消操作
        /// </summary>
        public void Cancel()
        {
            this.EditItemIndex = -1;
            BindData();
        }

        /// <summary>
        /// 通过SQL字符串更新表记录并重新绑定
        /// </summary>
        /// <param name="sqlstring"></param>
        public void Update(string sqlstring)
        {
            DbHelper.ExecuteNonQuery(CommandType.Text, sqlstring);
            this.EditItemIndex = -1;
            BindData();
        }

        /// <summary>
        /// 通过SQL字符串插入新记录并重新绑定
        /// </summary>
        /// <param name="sqlstring"></param>
        public void Insert(string sqlstring)
        {
            DbHelper.ExecuteNonQuery(CommandType.Text, sqlstring);
            this.EditItemIndex = -1;
            BindData();
        }


        private void PageIndex(int pageindex)
        {
            BindData();
        }

        /// <summary>
        /// 跳转指定分页
        /// </summary>
        /// <param name="value"></param>
        public void LoadCurrentPageIndex(int value)
        {
            this.CurrentPageIndex = (value < 0) ? 0 : value;
            BindData();
        }

        /// <summary>
        /// 数据列表项绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.Attributes.Add("onmouseover", "this.className='mouseoverstyle'");
                e.Item.Attributes.Add("onmouseout", "this.className='mouseoutstyle'");
                //e.Item.Style["cursor"] = "hand";
                //e.Item.Cells[3].Attributes.Add("onclick", "alert('你点击的ID是: " + e.Item.Cells[0].Text + "!');");
            }

            if (!this.SaveDSViewState)
            {
                this.Controls[0].EnableViewState = false;
            }

            if (this.IsFixConlumnControls)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    //int count=0;
                    for (int i = 0; i < e.Item.Cells.Count; i++)
                    {
                        if ((!e.Item.Cells[i].HasControls()))
                        {
                            if (GetBoundColumnFieldReadOnly()[i].ToString().ToLower() == "false") //判断是否存在只读属性
                            {
                                System.Web.UI.WebControls.TextBox t = new System.Web.UI.WebControls.TextBox();
                                t.ID = GetBoundColumnField()[i].ToString();
                                t.Attributes.Add("onmouseover", "if(this.className != 'FormFocus') this.className='FormBase'");
                                t.Attributes.Add("onmouseout", "if(this.className != 'FormFocus') this.className='formnoborder'");
                                t.Attributes.Add("onfocus", "this.className='FormFocus';");
                                t.Attributes.Add("onblur", "this.className='formnoborder';");
                                t.Attributes.Add("class", "formnoborder");
                                t.Text = e.Item.Cells[i].Text.Trim().Replace("&nbsp;", "");

                                //设置宽度
                                if (this.Columns[i].ItemStyle.Width.Value > 0)
                                {
                                    t.Width = (int)this.Columns[i].ItemStyle.Width.Value;
                                }
                                else
                                {
                                    t.Width = 100;
                                }
                                e.Item.Cells[i].Controls.Add(t);
                            }

                        }
                        else
                        {
                            foreach (System.Web.UI.Control c in e.Item.Cells[i].Controls)
                            {
                                //加载下拉控件
                                if (c is Rmlife.Control.DropDownList)
                                {
                                    Rmlife.Control.DropDownList __dropdownlist = (Rmlife.Control.DropDownList)c;
                                    if (__dropdownlist.SqlText != "")
                                    {
                                        __dropdownlist.AddTableData(__dropdownlist.SqlText);
                                    }

                                    try
                                    {
                                        __dropdownlist.SelectedValue = Convert.ToString(DataBinder.Eval(e.Item.DataItem, __dropdownlist.DataValueField));
                                    }
                                    catch
                                    { ;}
                                }

                                //加载普通下拉控件
                                if (c is System.Web.UI.WebControls.DropDownList)
                                {
                                    System.Web.UI.WebControls.DropDownList __dropdownlist = (System.Web.UI.WebControls.DropDownList)c;
                                    try
                                    {
                                        __dropdownlist.SelectedValue = Convert.ToString(DataBinder.Eval(e.Item.DataItem, __dropdownlist.DataValueField));
                                    }
                                    catch
                                    { ;}
                                }

                            }
                        }

                        // count++;
                    }
                }
            }
            else
            {
                if (e.Item.ItemType == ListItemType.EditItem)
                {
                    for (int i = 0; i < e.Item.Cells.Count; i++)
                    {
                        e.Item.Cells[i].BorderWidth = 1;
                        e.Item.Cells[i].BorderStyle = BorderStyle.Solid;
                        e.Item.Cells[i].BorderColor = System.Drawing.Color.FromArgb(234, 233, 225);
                        if (e.Item.Cells[i].HasControls())
                        {
                            for (int j = 0; j < e.Item.Cells[i].Controls.Count; j++)
                            {
                                System.Web.UI.WebControls.TextBox t = e.Item.Cells[i].Controls[j] as System.Web.UI.WebControls.TextBox;
                                if (t != null)
                                {
                                    t.Attributes.Add("onfocus", "this.className='FormFocus';");
                                    t.Attributes.Add("onblur", "this.className='FormBase';");
                                    t.Attributes.Add("class", "FormBase");
                                }
                            }
                        }
                    }
                }
            }
        }

        //protected void RowChanged(object sender, System.EventArgs e)
        //{
        //    DataGridItem dgi = (DataGridItem)(((Control)sender).NamingContainer);
        //    Label bookidlabel = (Label)dgi.Cells[0].Controls[1];
        //    int bookid = int.Parse(bookidlabel.Text);
        //    if (!bookidlist.Contains(bookid))
        //    {
        //        bookidlist.Add(bookid);
        //    }
        //}

        /// <summary>
        /// 装载选项按钮
        /// </summary>
        /// <param name="keyid"></param>
        /// <returns></returns>
        public string LoadSelectedCheckBox(string keyid)
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<INPUT id=\"keyid\"  type=\"checkbox\" value=\"" + keyid + "\"	name=\"keyid\" checked  style=\"display:none\">");
            return sb.ToString();
        }


        /// <summary>
        /// 得到当前页面的keyid字段的数组
        /// </summary>
        /// <returns></returns>
        public System.Collections.ArrayList GetKeyIDArray()
        {
            System.Collections.ArrayList al = new ArrayList();
            if (WebReq.GetString("keyid") != "")
            {
                foreach (string id in WebReq.GetString("keyid").Split(','))
                {
                    al.Add(id);
                }
            }
            return al;
        }


        /// <summary>
        /// 得到指定行的控件字段的值
        /// </summary>
        /// <param name="controlnumber">控件数</param>
        /// <param name="fieldname">字段名</param>
        /// <returns></returns>
        public string GetControlValue(int controlnumber, string fieldname)
        {
            return WebReq.GetFormString(this.ClientID.Replace("_", ":") + ":_ctl" + (controlnumber + 3) + ":" + fieldname);
        }


        /// <summary>
        /// 得到指定行的CheckBox控件字段的值
        /// </summary>
        /// <param name="controlnumber">控件数</param>
        /// <param name="fieldname">字段名</param>
        /// <returns></returns>
        public bool GetCheckBoxValue(int controlnumber, string fieldname)
        {
            string selectcontrolvalue = GetControlValue(controlnumber, fieldname);
            if (selectcontrolvalue == "on")
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到相应的字段列表
        /// </summary>
        /// <returns></returns>
        private ArrayList GetBoundColumnField()
        {
            System.Collections.ArrayList __arraylist = new ArrayList();
            foreach (DataGridColumn o in this.Columns)
            {
                System.Web.UI.WebControls.BoundColumn __boundcolumn = o as System.Web.UI.WebControls.BoundColumn;
                if (__boundcolumn != null)
                {
                    __arraylist.Add(__boundcolumn.DataField);
                }
                else
                {
                    System.Web.UI.WebControls.TemplateColumn __templatecolumn = o as System.Web.UI.WebControls.TemplateColumn;
                    __arraylist.Add(__templatecolumn.HeaderText);
                }
            }
            return __arraylist;
        }


        /// <summary>
        /// 得到相应的字段只读属性
        /// </summary>
        /// <returns></returns>
        private ArrayList GetBoundColumnFieldReadOnly()
        {
            System.Collections.ArrayList __arraylist = new ArrayList();
            foreach (DataGridColumn o in this.Columns)
            {
                System.Web.UI.WebControls.BoundColumn __boundcolumn = o as System.Web.UI.WebControls.BoundColumn;
                if (__boundcolumn != null)
                {
                    __arraylist.Add(__boundcolumn.ReadOnly);
                }
                else
                {
                    __arraylist.Add(true);
                }
            }
            return __arraylist;
        }

        /// <summary>
        /// 数据列表项创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DataGrid_ItemCreated(Object sender, DataGridItemEventArgs e)
        {
            ListItemType elemType = e.Item.ItemType;

            if (elemType == ListItemType.Pager)
            {
                TableCell cell1 = (TableCell)e.Item.Controls[0];
                //cell1.MergeStyle(DataGrid1.HeaderStyle); 
                //cell1.BackColor = Color.Navy; 
                //cell1.BorderWidth=0;
                //cell1.ColumnSpan = this.ColumnSpan; 
                cell1.HorizontalAlign = HorizontalAlign.Left;
                cell1.VerticalAlign = VerticalAlign.Bottom;
                cell1.CssClass = "datagridPager";

                LiteralControl splittable = new LiteralControl("splittable");
                splittable.Text = "</td></tr></table><table class=\"datagridpage\"><tr><td height=\"2\"></td></tr><tr><td>";
                cell1.Controls.AddAt(0, splittable);

                /*
                // 按钮
                GoToPagerInputText.ID = "GoToPagerInputText";
                GoToPagerInputText.Attributes.Add("runat", "server");
                GoToPagerInputText.Attributes.Add("onkeydown", "if(event.keyCode==13) { var gotoPageID=this.name.replace('InputText','Button'); return(document.getElementById(gotoPageID)).focus();}");
                GoToPagerInputText.Size = 6;
                GoToPagerInputText.Value = (this.CurrentPageIndex == 0) ? "1" : (this.CurrentPageIndex + 1).ToString();
                GoToPagerButton.ID = "GoToPagerButton";
                GoToPagerButton.Text = " Go ";

                //  cell1.Controls.Add(GoToPagerInputText);
                //cell1.Controls.Add(GoToPagerButton);

                LiteralControl p = new LiteralControl("p");
                p.Text = "";
                StringBuilder sb = new StringBuilder();
                sb.Append("<div class=\"pages_btns\">");
                sb.Append("<div class=\"pages\">");
                sb.Append("<em>");
                sb.Append("共");
                sb.Append(this.VirtualItemCount);
                sb.Append("条记录");
                sb.Append("</em>");
                sb.Append("<strong>");
                sb.Append((this.CurrentPageIndex + 1));
                sb.Append("页");
                sb.Append("</strong>");

                sb.Append("<span>1</span>");

                sb.Append("<kbd>");
                sb.Append("跳转");

                sb.Append("页");

                sb.Append("</kbd>");
                sb.Append("</em>");
                sb.Append("</div>");
                sb.Append("</div>");
                p.Text = sb.ToString();

                cell1.Controls.Add(p);
                */

                LiteralControl PageNumber = new LiteralControl("PageNumber");
                PageNumber.Text = " ";
                if (this.PageCount <= 1)
                {
                    try
                    {
                        cell1.Controls.RemoveAt(1); //当页数为1时, 则不显示页码
                    }
                    catch { ; }
                }
                else
                {
                    PageNumber.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                }

                PageNumber.Text += "<font color=black>共 " + this.PageCount + " 页, 当前第 " + (this.CurrentPageIndex + 1) + " 页";

                if (this.VirtualItemCount > 0)
                {
                    PageNumber.Text += ", 共 " + this.VirtualItemCount + " 条记录";
                }

                PageNumber.Text += "&nbsp;&nbsp;" + ((this.PageCount > 1) ? "跳转" : "");

                cell1.Controls.Add(PageNumber);

                //当大于1时显示跳转按钮
                if (this.PageCount > 1)
                {
                    //加载跳转文件框
                    GoToPagerInputText.ID = "GoToPagerInputText";
                    GoToPagerInputText.Attributes.Add("runat", "server");
                    GoToPagerInputText.Attributes.Add("onkeydown", "if(event.keyCode==13) { var gotoPageID=this.name.replace('InputText','Button'); return(document.getElementById(gotoPageID)).focus();}");
                    GoToPagerInputText.Size = 6;
                    GoToPagerInputText.Value = (this.CurrentPageIndex == 0) ? "1" : (this.CurrentPageIndex + 1).ToString();
                    cell1.Controls.Add(GoToPagerInputText);

                    PageNumber = new LiteralControl("PageNumber");
                    PageNumber.Text = "页&nbsp;&nbsp;";
                    cell1.Controls.Add(PageNumber);

                    //加载跳转按钮	
                    GoToPagerButton.ID = "GoToPagerButton";
                    GoToPagerButton.Text = " Go ";
                    cell1.Controls.Add(GoToPagerButton);
                }


                e.Item.Controls.Add(cell1);

                TableCell pager = (TableCell)e.Item.Controls[0];

                for (int i = 1; i < pager.Controls.Count; i += 2)
                {
                    Object o = pager.Controls[i];

                    if (o is LinkButton)
                    {
                        LinkButton h = (LinkButton)o;

                        if (h.Text == "..." && i == 1)//pager.Controls[i].ID == "_ctl0")
                        {
                            h.Text = "上一页";
                            continue;
                        }
                        if (i > 1 && h.Text == "...")
                        {
                            h.Text = "下一页";
                            continue;
                        }

                        //h.Attributes.Add("href", "javascript:__doPostBack('TabControl1$tabPage1$DataGrid1$_ctl29$_ctl"+(Utils.StrToInt(h.Text)-1)+"','');");
                        //h.Attributes.Add("onclick", "javascript:__doPostBack('TabControl1$tabPage1$DataGrid1$_ctl29$_ctl1','');");
                        //h.Attributes.Add("onclick", "javascript:document.getElementById('Layer5').innerHTML ='<br /><table><tr><td valign=top><img border=\"0\" src=\"../images/ajax_loading.gif\"  /></td><td valign=middle style=\"font-size: 14px;\" >正在加载数据...<BR /></td></tr></table>';document.getElementById('success').style.display ='block';");
                    }
                    if (o is Label)
                    {
                        Label l = (Label)o;
                        if (l.Text == "..." && i == 1)//l.ID == "_ctl0") 
                        {
                            l.Text = "上一页";
                        }

                        if (i > 1 && l.Text == "...")
                        {
                            l.Text = "下一页";
                        }
                    }
                }

            }
            else
            {
                if ((elemType == ListItemType.AlternatingItem) || (elemType == ListItemType.Item) || (elemType == ListItemType.Header))
                {
                    foreach (System.Web.UI.Control control in e.Item.Controls)
                    {
                        TableCell cell = (TableCell)control;
                        cell.BorderWidth = 1;
                        cell.BorderColor = System.Drawing.Color.FromArgb(234, 233, 225);
                        //下面的代码暂时不可用
                        //System.Web.UI.WebControls.Style s=new System.Web.UI.WebControls.Style();
                        //s.CssClass="datagridItemtd";
                        //cell.ApplyStyle(s);
                    }
                }

                for (int i = 0; i < e.Item.Cells.Count; i++)
                {
                    if (elemType == ListItemType.Header)
                    {
                        e.Item.Cells[i].Wrap = false;
                    }
                    else
                    {
                        if (i >= 2)
                        {
                            e.Item.Cells[i].Wrap = true;
                        }
                        else
                        {
                            e.Item.Cells[i].Wrap = false;
                        }
                    }
                }
            }

        }

        #region Property SaveDSViewState
        /// <summary>
        /// 是否保存数据的ViewState值
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public bool SaveDSViewState
        {
            get
            {
                object obj = ViewState["SaveDSViewState"];
                if (obj == null) return false;
                else
                {
                    if (obj.ToString().ToLower() == "true")
                        return true;
                    else
                        return false;
                }
            }
            set
            {
                ViewState["SaveDSViewState"] = value;
            }
        }
        #endregion

        public DataTable SourceDataTable
        {
            get
            {
                return (DataTable)ViewState["SourceDataTable"];
            }
            set
            {
                ViewState["SourceDataTable"] = value;
            }
        }

        #region Property SqlText
        /// <summary>
        /// 使用的查询字符串
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public string SqlText
        {
            get
            {
                return (string)ViewState["SqlText"];
            }
            set
            {
                ViewState["SqlText"] = value;
            }
        }
        #endregion


        #region Property ColumnSpan
        /// <summary>
        /// ColumnSpan数,用于在分页底部设置表格的colspan属性
        /// </summary>
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public int ColumnSpan
        {
            get
            {
                object obj = ViewState["ColumnSpan"];
                return obj == null ? 1 : (int)obj;
            }

            set
            {
                ViewState["ColumnSpan"] = value;
            }
        }
        #endregion


        #region Property TableHeaderName

        /// <summary>
        /// 表名称。
        /// </summary>
        [Description("表标题名称。"), Category("Appearance"), DefaultValue("")]
        public string TableHeaderName
        {
            get
            {

                object obj = ViewState["TableHeaderName"];
                return obj == null ? "" : (string)obj;
            }
            set
            {
                ViewState["TableHeaderName"] = value;
            }
        }

        #endregion


        #region Property ImagePath
        /// <summary>
        /// 图片所在的文件路径。
        /// </summary>
        [Description("表头的名称。"), Category("Appearance"), DefaultValue("")]
        public string ImagePath
        {
            get
            {

                object obj = ViewState["ImagePath"];
                return obj == null ? "../images/" : (string)obj;
            }
            set
            {
                ViewState["ImagePath"] = value;
            }
        }

        #endregion


        #region Property IsFixConlumnControls
        /// <summary>
        /// 是否安插列控件。
        /// </summary>
        [Description("是否在列表页中载入列控件"), Category("Appearance"), DefaultValue(false)]
        public bool IsFixConlumnControls
        {
            get
            {

                object o = ViewState["IsFixConlumnControls"];
                if ((o != null) && (o.ToString().ToLower() == "true"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                ViewState["IsFixConlumnControls"] = value;
            }
        }
        #endregion


        /// <summary> 
        /// 输出html来在浏览器中显示控件
        /// </summary>
        /// <param name="output"> 要写出到的 HTML 编写器 </param>
        protected override void Render(HtmlTextWriter output)
        {

            output.WriteLine("<table class=\"ntcplist\" >\r\n");
            if (!string.IsNullOrEmpty(TableHeaderName))
            {
                output.WriteLine("<tr class=\"head\">\r\n");
                output.WriteLine("<td>" + this.TableHeaderName + "</td>\r\n");
                output.WriteLine("</tr>\r\n");
            }
            output.WriteLine("<tr>\r\n");
            output.WriteLine("<td>\r\n");

            base.Render(output);

            output.WriteLine("</td></tr></TABLE>");
        }

        /// <summary>
        /// 重写数据源属性
        /// </summary>
        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                base.DataSource = value;

                //当不是手工定制分页时(因为手工定制分页时会在前台指定VirtualItemCount数值)
                if (!this.AllowCustomPaging)
                {
                    if (value is DataTable)
                    {
                        this.VirtualItemCount = (value as DataTable).Rows.Count;
                    }

                    if (value is DataSet)
                    {
                        DataSet ds = value as DataSet;
                        if (ds.Tables.Count > 0)
                        {
                            this.VirtualItemCount = ds.Tables[0].Rows.Count;
                        }
                    }

                    //当为数组类型时
                    if (value.GetType().Name.ToString().IndexOf("[]") > 0)
                    {
                        Array array = value as Array;
                        if (array.Length > 0)
                        {
                            this.VirtualItemCount = array.Length;
                        }
                    }
                }
            }
        }

        #region "自定义排序事件"
        public sealed class RecssDataGridSortCommandEventArgs : EventArgs
        {
            private string sortExpression;
            private string sortOrder;

            public RecssDataGridSortCommandEventArgs(object commandSource, string sortExpression, string sortOrder)
            {
                this.sortExpression = sortExpression;
                this.sortOrder = sortOrder;
            }
            public string SortExpression
            {
                get
                {
                    return this.sortExpression;
                }
            }
            public string SortOrder
            {
                get
                {
                    return this.sortOrder;
                }
            }
        }
        private string GetSortExpression(string[] sortFields, string sortOrder)
        {
            string str = sortOrder;

            string str2 = "";
            str2 = sortFields[0] + " " + str;
            for (int i = 1; i < sortFields.Length; i++)
            {
                string str3 = str2;
                str2 = str3 + "," + sortFields[i] + " " + str;
            }
            return str2;
        }
        public delegate void RecssDataGridSortCommandEventHandler(object sender, RecssDataGridSortCommandEventArgs e);
        new public event RecssDataGridSortCommandEventHandler SortCommand;

        protected virtual void OnSortCommand(RecssDataGridSortCommandEventArgs e)
        {
            if (this.SortCommand != null)
            {
                this.SortCommand(this, e);
            }
        }

        #endregion

        #region "重写分页"
        /*
        protected override void InitializePager(DataGridItem item, int columnSpan, PagedDataSource pagedDataSource)
        {
            base.InitializePager(item, columnSpan, pagedDataSource);


            if (this.AllowPaging )
            {
                item.Cells[0].Controls.Clear();
                if (this.PagerStyle.Mode == PagerMode.NextPrev)
                {
                    if (base.PageCount > 1)
                    {
                        Table child = new Table();
                        child.Rows.Add(new TableRow());
                        child.Rows[0].Cells.Add(new TableCell());
                        child.Rows[0].Cells.Add(new TableCell());
                        child.BorderWidth = 0;
                        child.CellSpacing = 0;
                        child.CellPadding = 0;
                        child.Style.Add("width", "100%");
                        child.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Left;
                        child.Rows[0].Cells[0].VerticalAlign = VerticalAlign.Middle;
                        child.Rows[0].Cells[1].HorizontalAlign = HorizontalAlign.Right;
                        child.Rows[0].Cells[1].VerticalAlign = VerticalAlign.Middle;
                        if (base.CurrentPageIndex > 0)
                        {
                            LinkButton button = new LinkButton
                            {
                                CssClass = "ResultLink",
                                Text = string.Format(this.PagerStyle.PrevPageText, this.PageSize),
                                CausesValidation = false,
                            };
                            button.Click += new EventHandler(this.prevLinkBtn_Click);
                            child.Rows[0].Cells[0].Controls.Add(button);
                        }
                        if (base.CurrentPageIndex < (base.PageCount - 1))
                        {
                            LinkButton button2 = new LinkButton
                            {
                                CssClass = "ResultLink",
                                Text = string.Format(this.PagerStyle.NextPageText, this.PageSize),
                                CausesValidation = false,
                            };
                            button2.Click += new EventHandler(this.nextLinkBtn_Click);
                            child.Rows[0].Cells[1].Controls.Add(button2);
                        }
                        item.Cells[0].Controls.Add(child);
                    }
                    else
                    {
                        item.Visible = false;
                    }
                }
                else
                {
                    int virtualItemCount;
                    int num2;
                    int num3;
                    Table table2 = new Table();
                    table2.Rows.Add(new TableRow());
                    table2.Rows[0].Cells.Add(new TableCell());
                    table2.BorderWidth = 0;
                    table2.CellSpacing = 0;
                    table2.CellPadding = 0;
                    table2.Style.Add("width", "100%");
                    table2.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Left;
                    table2.Rows[0].Cells[0].VerticalAlign = VerticalAlign.Middle;
                    table2.Rows[0].Cells[0].CssClass = "ResultTd";
                    if (this.AllowCustomPaging)
                    {
                        virtualItemCount = this.InsertingProperties.VirtualItemCount;
                    }
                    else
                    {
                        DataView table3 = (DataView)this.DataSource;
                        if (table3 == null)
                        {
                            virtualItemCount = 0;
                        }
                        else
                        {
                            virtualItemCount = table3.Count;
                        }
                    }
                    if (virtualItemCount == 0)
                    {
                        num2 = 0;
                        num3 = 0;
                    }
                    else
                    {
                        num2 = (this.InsertingProperties.PageSize * base.CurrentPageIndex) + 1;
                        if (num2 < 1)
                        {
                            num2 = 1;
                        }
                        num3 = this.InsertingProperties.PageSize * (base.CurrentPageIndex + 1);
                        if (num3 > virtualItemCount)
                        {
                            num3 = virtualItemCount;
                        }
                    }
                    Label label = new Label
                    {
                        Text = "总记录数" + virtualItemCount.ToString() + "条&nbsp;",
                        CssClass = "ResultBfont"
                    };
                    table2.Rows[0].Cells[0].Controls.Add(label);
                    table2.Rows[0].Cells[0].Controls.Add(new LiteralControl("当前为第"));
                    Label label2 = new Label
                    {
                        Text = num2.ToString() + "-" + num3.ToString() + "条&nbsp;",
                        CssClass = "ResultBfont"
                    };
                    table2.Rows[0].Cells[0].Controls.Add(label2);
                    table2.Rows[0].Cells[0].Controls.Add(new LiteralControl("页&nbsp;"));
                    if (base.CurrentPageIndex > 0)
                    {
                        LinkButton button3 = new LinkButton
                        {
                            CssClass = "ResultLink",
                            Text = string.Format(this.PagerStyle.PrevPageText, this.PageSize),
                            CausesValidation = false,
                        };
                        button3.Click += new EventHandler(this.prevLinkBtn_Click);
                        table2.Rows[0].Cells[0].Controls.Add(button3);
                        table2.Rows[0].Cells[0].Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
                    }
                    int num4 = (base.CurrentPageIndex + 1) - ((this.PagerStyle.PageButtonCount - 1) / 2);
                    if (num4 <= 0)
                    {
                        num4 = 1;
                    }
                    int pageCount = num4 + (this.PagerStyle.PageButtonCount - 1);
                    if (pageCount > base.PageCount)
                    {
                        pageCount = base.PageCount;
                    }
                    if ((pageCount - (this.PagerStyle.PageButtonCount - 1)) < num4)
                    {
                        num4 = pageCount - (this.PagerStyle.PageButtonCount - 1);
                        if (num4 <= 0)
                        {
                            num4 = 1;
                        }
                    }
                    for (int i = num4; i <= pageCount; i++)
                    {
                        if (i == (base.CurrentPageIndex + 1))
                        {
                            Label label3 = new Label
                            {
                                CssClass = "CurrentPage",
                                Text = i.ToString()
                            };
                            table2.Rows[0].Cells[0].Controls.Add(label3);
                            table2.Rows[0].Cells[0].Controls.Add(new LiteralControl("&nbsp;"));
                        }
                        else
                        {
                            LinkButton button4 = new LinkButton
                            {
                                CssClass = "PagerLink",
                                Text = i.ToString(),
                                CausesValidation = false,
                            };
                            button4.Click += new EventHandler(this.customLinkBtn_Click);
                            table2.Rows[0].Cells[0].Controls.Add(button4);
                            table2.Rows[0].Cells[0].Controls.Add(new LiteralControl("&nbsp;"));
                        }
                    }
                    if (base.CurrentPageIndex < (base.PageCount - 1))
                    {
                        LinkButton button5 = new LinkButton
                        {
                            CssClass = "ResultLink",
                            Text = string.Format(this.PagerStyle.NextPageText, this.PageSize),
                            CausesValidation = false,
                        };
                        table2.Rows[0].Cells[0].Controls.Add(new LiteralControl("&nbsp;"));
                        button5.Click += new EventHandler(this.nextLinkBtn_Click);
                        table2.Rows[0].Cells[0].Controls.Add(button5);
                    }
                    item.Cells[0].Controls.Add(table2);
                }
            }
        }
        protected virtual void prevLinkBtn_Click(object sender, EventArgs e)
        {
            int newPageIndex = base.CurrentPageIndex - 1;
            DataGridPageChangedEventArgs args = new DataGridPageChangedEventArgs(this, newPageIndex);
            base.OnPageIndexChanged(args);
        }

        protected virtual void nextLinkBtn_Click(object sender, EventArgs e)
        {
            int newPageIndex = base.CurrentPageIndex + 1;
            DataGridPageChangedEventArgs args = new DataGridPageChangedEventArgs(this, newPageIndex);
            base.OnPageIndexChanged(args);
        }

        protected virtual void customLinkBtn_Click(object sender, EventArgs e)
        {
            int newPageIndex = Convert.ToInt32(((LinkButton)sender).Text) - 1;
            DataGridPageChangedEventArgs args = new DataGridPageChangedEventArgs(this, newPageIndex);
            base.OnPageIndexChanged(args);
        }

        private static DataTable ConvertToDataTable(DataTable DataTable)
        {
            DataTable table = new DataTable();
            foreach (DataColumn column in DataTable.Columns)
            {
                table.Columns.Add(column.ColumnName, column.DataType);
            }
            foreach (DataRow row in DataTable.Rows)
            {
                DataRow row2 = table.NewRow();
                for (int i = 0; i < DataTable.Columns.Count; i++)
                {
                    row2[i] = row[i];
                }
                table.Rows.Add(row2);
            }
            return table;
        }
        private bool IsAddingNewRow = false;
        public virtual PropertiesAtInsertCommand InsertingProperties
        {
            get
            {
                return new PropertiesAtInsertCommand(this.IsAddingNewRow, base.VirtualItemCount, base.PageSize, base.PageCount);
            }
        }

        public class PropertiesAtInsertCommand
        {
            private bool isInsertCommand;
            private int pageSize;
            private int virtualItemCount;

            public PropertiesAtInsertCommand(bool isInsertCommand, int virtualItemCount, int pageSize, int pageCount)
            {
                this.isInsertCommand = isInsertCommand;
                if (this.isInsertCommand)
                {
                    this.virtualItemCount = virtualItemCount - pageCount;
                    this.pageSize = pageSize - 1;
                }
                else
                {
                    this.virtualItemCount = virtualItemCount;
                    this.pageSize = pageSize;
                }
            }

            public virtual bool IsInsertCommand
            {
                get
                {
                    return this.isInsertCommand;
                }
            }

            public virtual int PageSize
            {
                get
                {
                    return this.pageSize;
                }
            }

            public virtual int VirtualItemCount
            {
                get
                {
                    return this.virtualItemCount;
                }
            }
      

        }
         */
        #endregion

    }


}
